﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace DataAccess
{
    public class CommentsRepository : ConnectionClass
    {
        public CommentsRepository() : base() { }

        public IEnumerable<Comment> GetCommentsForProduct(int productID)
        {
            return entities.Comments.Where(c => c.Product_FK == productID);
        }

        public void PlaceComment(Comment thisComment)
        {
            try
            {
                entities.AddToComments(thisComment);
                entities.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex.InnerException;
            }
        }

        public bool HasUserPlacedComment(string username, int pID)
        {
            if (entities.Comments.Count(c => c.Username_FK == username && c.Product_FK == pID) == 0)
            {
                return false;
            }
            else return true;
        }

        public int GetNoOfProductComments(int productID)
        {
            return entities.Comments.Count(c => c.Product_FK == productID);
        }
    }
}
